package cn.itcast.goods.controller;

import cn.itcast.annotation.Permission;
import cn.itcast.constant.PermissionConstant;
import cn.itcast.goods.dto.DishDTO;
import cn.itcast.goods.dto.DishPageQueryDTO;
import cn.itcast.goods.mapper.DishMapper;
import cn.itcast.goods.pojo.Dish;
import cn.itcast.goods.service.DishService;
import cn.itcast.goods.vo.DishOverViewVO;
import cn.itcast.goods.vo.DishVO;
import cn.itcast.result.PageBean;
import cn.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping({"/admin/dish","/user/dish"})
@Api(tags = "菜品相关接口")
@Slf4j
@Permission(value = PermissionConstant.DISH_MANAGEMENT_PERMISSIONS)
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishMapper dishMapper;


    // ============================

    /**
     * 查询不同状态菜品的数量
     *
     * @return
     */
    @GetMapping("/feign/getOverviewDishes")
    public DishOverViewVO getOverviewDishes(){
        return dishMapper.getOverviewDishes();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/listAll")
    public Result<List<DishVO>> getByCategoryId(Integer categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        List<DishVO> dishes = dishService.getDishVOByCategoryId(categoryId);

        return Result.success(dishes);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> getDishByCategoryId(Integer categoryId) {
        log.info("根据分类id查询菜品:{}", categoryId);
        List<Dish> dishs = dishService.getByCategoryId(categoryId);

        return Result.success(dishs);
    }

    /**
     * 根据DishId查询Dish
     *
     * @param dishId
     * @return
     */

    @ApiOperation("根据DishId查询Dish")
    @PostMapping("/dish")
    public Result<Dish> getDishByDishId(@RequestBody Long dishId){
        Dish dish = dishService.getById(dishId);
        return Result.success(dish);
    }

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        dishService.saveWithFlavor(dto);

        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageBean> pageQuery(DishPageQueryDTO dto) {
        PageBean pageBean = dishService.pageQuery(dto);

        return Result.success(pageBean);
    }


    /**
     * 批量删除菜品
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result<String> delete(@RequestParam("ids") List<Integer> ids) {
        dishService.delete(ids);

        return Result.success();
    }

    /**
     * 根据id查询菜品和口味
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品和口味")
    @GetMapping("/{id}")
    public Result<DishVO> getByIdWithFlavor(@PathVariable Integer id) {
        log.info("根据id查询菜品和口味{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);

        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto) {
        dishService.update(dto);

        return Result.success();
    }

    /**
     * 菜品起售、停售
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    public Result startOrStop(Long id, @PathVariable Integer status) {
        dishService.statrtOrStop(id, status);

        return Result.success();
    }

}
